package com.fqyd.campustransaction.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.fqyd.campustransaction.mapper.TaskMapper;
import com.fqyd.campustransaction.model.Task;
import com.fqyd.campustransaction.model.TaskExample;
import com.fqyd.campustransaction.service.TaskService;
import com.fqyd.campustransaction.utils.Tools;

/**
 * 任务
 */
@Service
public class TaskServiceImpl implements TaskService {

	@Resource
	private TaskMapper mapper;

	@Override
	public List<Task> getAll() {
		TaskExample example = new TaskExample();
		TaskExample.Criteria criteria = example.createCriteria();
		criteria.andStatusEqualTo("审核通过");
		return mapper.selectByExample(example);
	}

	@Override
	public void add(Task goods) {
		mapper.insert(goods);
	}

	@Override
	public void update(Task goods) {
		mapper.updateByPrimaryKey(goods);
	}

	@Override
	public void delete(String id) {
		mapper.deleteByPrimaryKey(id);
	}

	@Override
	public Task getById(String id) {
		return mapper.selectByPrimaryKey(id);
	}

	@Override
	public List<Task> getByTypeId(String typeId) {
		TaskExample example = new TaskExample();
		TaskExample.Criteria criteria = example.createCriteria();
		criteria.andTypeIdEqualTo(typeId);
		criteria.andStatusEqualTo("审核通过");
		return mapper.selectByExample(example);
	}

	@Override
	public List<Task> getByUserId(String userId) {
		TaskExample example = new TaskExample();
		TaskExample.Criteria criteria = example.createCriteria();
		criteria.andUserIdEqualTo(userId);
		return mapper.selectByExample(example);
	}

	@Override
	public List<Task> getByAudit() {
		TaskExample example = new TaskExample();
		TaskExample.Criteria criteria = example.createCriteria();
		criteria.andStatusEqualTo("待审核");
		return mapper.selectByExample(example);
	}

	@Override
	public List<Task> search(String typeId, String key, String sort) {
		TaskExample example = new TaskExample();
		TaskExample.Criteria criteria = example.createCriteria();
		criteria.andStatusEqualTo("审核通过");
		if (Tools.isNotEmpty(typeId)) {
			criteria.andTypeIdEqualTo(typeId);
		}
		if (Tools.isNotEmpty(key)) {
			criteria.andNameLike("%" + key + "%");
		}
		if (Tools.isNotEmpty(sort)) {
			if ("0".equals(sort)) {
				example.setOrderByClause("id ASC");
			}
			if ("1".equals(sort)) {
				example.setOrderByClause("id DESC");
			}
			if ("2".equals(sort)) {
				example.setOrderByClause("price ASC");
			}
		}
		return mapper.selectByExample(example);
	}

	@Override
	public List<Task> getByUserAndOrderId(Task task) {
		return mapper.getByUserAndOrderId(task);
	}
}
